<?php

namespace app\admin\controller;


use app\common\base\AuthenticatedController;
use app\common\base\Form;
use think\Db;
use think\Error;

/**
 * 以旧换新管理控制器
 * Class DeviceOldController
 * @package app\admin\controller
 */
class DeviceOldController extends AuthenticatedController {


    /**
     *  添加以旧换新设备
     * @return \think\response\Json
     */
    public function Add() {

        Form::Validate([
            'name'                  =>  'require',
            'image'                 =>  'require',
            'device_attr'           =>  'require',
            'price'                 =>  'require',
        ]);

        $data = [
            'name'                  =>  input('name'),
            'image'                 =>  input('image'),
            'device_attr'           =>  input('device_attr'),
            'price'                 =>  input('price'),
        ];

        $result = Db::table('device_old')->insert($data);
        if (!$result) {
            return Error('添加失败');
        }

        return Success('添加设备成功');

    }


    /**
     * 删除以旧换新设备
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function Delete() {

        Form::Validate([
            'id'  =>  'require',
        ]);

        if (!Db::table('device_old')->where('id', input('id'))->delete()) {
            return Error('删除失败');
        }

        return Success('删除成功');

    }


    /**
     * 修改以旧换新设备
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function Modify() {

        Form::Validate([
            'id'                    =>  'require|integer',
            'name'                  =>  'require',
            'image'                 =>  'require',
            'device_attr'           =>  'require',
            'price'                 =>  'require',
        ]);

        $data = [
            'name'                  =>  input('name'),
            'image'                 =>  input('image'),
            'device_attr'           =>  input('device_attr'),
            'price'                 =>  input('price'),
        ];

        if (Db::table('device_old')->where('id', input('id'))->update($data) === false) {
            return Error('更新失败');
        }

        return Success('更新成功');
    }


    /**
     * 获取以旧换新列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GetList() {
        $result = Db::table('device_old')->select();

        return Success('获取成功', $result);
    }


    /**
     * 根据ID获取依旧换新设备
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GetOne() {
        Form::Validate([
            'id'    =>  'require|integer'
        ]);

        $result = Db::table('device_old')->where('id', input('id'))->find();
        if (!$result) {
            return Error('获取失败');
        }

        return Success('获取成功', $result);

    }

}